import java.util.Arrays;

class Solution1 {
    public int[][] merge(int[][] intervals) {
        int n = intervals.length;
        Arrays.sort(intervals,(a,b)->a[0]-b[0]);

        int[][] ret = new int[n][2];
        int index = 0;
        int left = intervals[0][0], right = intervals[0][1];
        for(int i = 1; i<n; i++) {
            if(intervals[i][0] <= right) {
                right = Math.max(right,intervals[i][1]);
            }else {
                ret[index][0] = left;
                ret[index][1] = right;
                index++;
                left = intervals[i][0];
                right = intervals[i][1];
            }
        }

        ret[index][0] = left;
        ret[index][1] = right;

        return Arrays.copyOf(ret,index+1);
    }
}

class Solution {
    public int eraseOverlapIntervals(int[][] intervals) {
        int n = intervals.length;
        Arrays.sort(intervals, (a,b)->b[0]-a[0]);
        int count = 0;
        int right = intervals[0][1];
        for(int i = 1; i<n; i++) {
            if(intervals[i][0] < right) {
                right = Math.min(right, intervals[i][1]);
                count++;
            }else {
                right = intervals[i][1];
            }
        }
        return count;
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Good code is its own best documentation.");
    }
}
